package com.example.baseproject.login;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.azhon.appupdate.manager.DownloadManager;
import com.blankj.utilcode.util.LogUtils;
import com.example.baseproject.R;
import com.example.baseproject.databinding.ActivityTwoBinding;
import com.example.baseproject.easyhttptest.TestViewModel;
import com.example.baseproject.main.api.MatchApi;
import com.example.baseproject.main.bean.TestOneBean;
import com.hjq.http.EasyHttp;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.OnHttpListener;
import com.tjcd.project.library_base.base.activity.BaseActivity;
import com.tjcd.project.library_base.base.bean.BaseHttpBean;

public class TwoActivity extends BaseActivity<ActivityTwoBinding, TestViewModel> {


    @Override
    protected Class<TestViewModel> setViewModelClass() {
        return TestViewModel.class;
    }

    @Override
    protected void init() {
        setClickHidekeyboard(((ViewGroup) findViewById(android.R.id.content)).getChildAt(0));
        vBinding.tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("key","返回值啊啊啊啊啊啊");
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        DownloadManager.Builder builder = new DownloadManager.Builder(this);
        builder.build();
        builder.build().download();

        vModel.getTestOneBeanLiveData().observe(this, resource -> {
            switch (resource.getStatus()) {
                case START:
                    // 显示加载中提示
                    LogUtils.e("数据错乱one", resource.getStatus());
                    break;
                case DATA_SUCCESS:
                    // 更新UI，显示匹配数据
                    if (resource.getData() != null) {
                        LogUtils.e("数据错乱one", resource.getStatus(), resource.getData().toString());
                    }else {
                        LogUtils.e("数据错乱one", resource.getStatus(), "数据为空");
                    }
                    break;
                case ERROR:
                    // 显示错误信息
                    LogUtils.e("数据错乱one", resource.getStatus(), resource.getMsg());
                    break;
                case FINISH:
                    // 请求完成，隐藏加载提示
                    LogUtils.e("数据错乱one", resource.getStatus());
                    break;
            }
        });
//        vModel.testOneBean();
        EasyHttp.get(ApplicationLifecycle.getInstance())
                .tag("关闭页面,继续请求")
                .api(new MatchApi())
                .delay(3000)
                .request(new OnHttpListener<BaseHttpBean<TestOneBean>>() {
                    @Override
                    public void onHttpStart(@NonNull IRequestApi api) {
                        OnHttpListener.super.onHttpStart(api);
                        LogUtils.e("数据错乱one", "开始请求");
                    }


                    @Override
                    public void onHttpSuccess(BaseHttpBean<TestOneBean> result) {
                        if (result.getData() != null){
                            // todo 是否需要拍的code 和 数据位空
                            LogUtils.e("数据错乱one", result.getData().toString());
                        }else {
                            LogUtils.e("数据错乱one", "数据为空");
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        LogUtils.e("数据错乱one 错误", throwable.getMessage());
                    }

                    @Override
                    public void onHttpEnd(@NonNull IRequestApi api) {
                        OnHttpListener.super.onHttpEnd(api);
                        LogUtils.e("数据错乱one", "请求结束");
                    }
                });

    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_two;
    }



}